import pygame
import random
from fruit import Fruit
from blade import Blade
from bomb import Bomb

class Game:
    def __init__(self, screen):
        self.screen = screen
        self.width, self.height = screen.get_size()
        
        # 游戏元素
        self.fruits = []
        self.bombs = []
        self.blade = Blade()
        self.score = 0
        self.lives = 3
        self.game_over = False
        
        # 计时器
        self.spawn_timer = 0
        self.spawn_interval = 1000  # 毫秒
        self.last_spawn_time = pygame.time.get_ticks()
        
        # 字体
        self.font = pygame.font.Font(None, 40)

    def handle_event(self, event):
        if self.game_over:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
                self.reset()
            return
        elif event.type == pygame.QUIT:
            return False
        elif event.type == pygame.MOUSEMOTION:
            # 鼠标滑过模式：鼠标移动就更新位置并自动保持切割状态
            self.blade.update_pos(event.pos)
        return True

    def spawn_fruit(self):
        now = pygame.time.get_ticks()
        if now - self.last_spawn_time > self.spawn_interval:
            # 随机生成水果或炸弹
            if random.random() < 0.8:  # 80%概率生成水果
                self.fruits.append(Fruit(self.width, self.height))
            else:  # 20%概率生成炸弹
                self.bombs.append(Bomb(self.width, self.height))
            
            self.last_spawn_time = now
            # 逐渐加快生成速度
            self.spawn_interval = max(500, self.spawn_interval - 1)

    def check_collisions(self):
        # 调试：打印当前轨迹长度
        # print("轨迹长度: {}, 切割状态: {}".format(len(self.blade.trail), self.blade.cutting))
        
        # 检查水果切割
        for fruit in self.fruits[:]:
            if self.blade.check_cut(fruit.rect) and not fruit.cut:
                fruit.cut = True
                self.score += 10
                print("水果被切割! 当前分数: {}".format(self.score))
                # 播放切割音效（可自行添加）
        
        # 检查炸弹碰撞
        for bomb in self.bombs[:]:
            if self.blade.check_cut(bomb.rect):
                self.lives -= 1
                print("切到炸弹! 当前生命: {}".format(self.lives))
                self.bombs.remove(bomb)
                if self.lives <= 0:
                    self.game_over = True

        # 检查未切割的水果落地
        for fruit in self.fruits[:]:
            if fruit.y > self.height + 50 and not fruit.cut:
                self.fruits.remove(fruit)
                self.lives -= 1
                print("水果落地! 当前生命: {}".format(self.lives))
                if self.lives <= 0:
                    self.game_over = True

        # 移除已落地的炸弹
        for bomb in self.bombs[:]:
            if bomb.y > self.height + 50:
                self.bombs.remove(bomb)

    def update(self):
        if not self.game_over:
            self.spawn_fruit()
            self.blade.update()
            
            # 更新水果
            for fruit in self.fruits:
                fruit.update()
            
            # 更新炸弹
            for bomb in self.bombs:
                bomb.update()
            
            self.check_collisions()

    def draw(self):
        # 绘制水果
        for fruit in self.fruits:
            fruit.draw(self.screen)
        
        # 绘制炸弹
        for bomb in self.bombs:
            bomb.draw(self.screen)
        
        # 绘制刀刃轨迹
        self.blade.draw(self.screen)
        
        # 绘制分数和生命
        score_text = self.font.render("分数: {}".format(self.score), True, (255, 255, 255))
        lives_text = self.font.render("生命: {}".format(self.lives), True, (255, 255, 255))
        self.screen.blit(score_text, (20, 20))
        self.screen.blit(lives_text, (self.width - 120, 20))
        
        # 游戏结束画面
        if self.game_over:
            over_text = self.font.render("游戏结束! 按R键重新开始", True, (255, 0, 0))
            self.screen.blit(over_text, (self.width//2 - 200, self.height//2))

    def reset(self):
        self.fruits = []
        self.bombs = []
        self.score = 0
        self.lives = 3
        self.game_over = False
        self.spawn_interval = 1000
        self.last_spawn_time = pygame.time.get_ticks()